﻿using EPiAbstractions.Common.Categories;
using EPiServer.Common.Categories;
using JoelAbrahamsson.TestUtilities;
using Moq;
using StructureMap.AutoMocking;
using Xunit;

namespace JoelAbrahamsson.BlogExtensions.Tests.BlogCategoryProviderTests
{
    public class ParseCategoryTests
    {
        [Fact]
        public void GivenValidTitle_ParseCategory_ReturnsMatchingCategory()
        {
            Category parentCategory = new Category(RandomValues.String);
            Mock<ICategory> fakeChildCategory = new Mock<ICategory>();
            fakeChildCategory.Setup(category => category.Parent).Returns(parentCategory);
            fakeChildCategory.Object.Name = RandomValues.String;
            string validTitle = parentCategory.Name + BlogCategoryProvider.CategoryNameDelimiter + fakeChildCategory.Object.Name;
            MoqAutoMocker<BlogCategoryProvider> blogCategoryProviderAutoMocker = new MoqAutoMocker<BlogCategoryProvider>();
            Mock<ICategoryHandlerFacade> fakeCategoryHandlerFacade = Mock.Get(blogCategoryProviderAutoMocker.Get<ICategoryHandlerFacade>());
            string categoryPath = BlogCategoryProvider.BlogCategoriesRootName 
                + BlogCategoryProvider.CategoryNameDelimiter 
                + parentCategory.Name
                + BlogCategoryProvider.CategoryNameDelimiter
                + fakeChildCategory.Object.Name;
            fakeCategoryHandlerFacade.Setup(handler => handler.GetCategory(categoryPath)).Returns(fakeChildCategory.Object);

            ICategory returnedCategory = blogCategoryProviderAutoMocker.ClassUnderTest.ParseCategory(validTitle);

            Assert.Equal<ICategory>(fakeChildCategory.Object, returnedCategory);
        }

        [Fact]
        public void GivenInvalidTitle_ParseCategory_ReturnsNull()
        {
            string invalidPath = RandomValues.String.Replace(BlogCategoryProvider.CategoryNameDelimiter, string.Empty);
            BlogCategoryProvider blogCategoryProvider = new MoqAutoMocker<BlogCategoryProvider>().ClassUnderTest;

            ICategory returnedCategory = blogCategoryProvider.ParseCategory(invalidPath);

            Assert.Null(returnedCategory);
        }
    }
}
